const state = () => ({
    product_classify: [], //商品分类
    product_Brand: [], //商品品牌
    new_arrivals_count: 0,
});
const getters = {
    getProductClassify: (state) => state.product_classify,
    getProductBrand: (state) => state.product_Brand,
    getNewArrivals: (state) => state.new_arrivals_count,
};
const mutations = {
    // SET_PRODUCT_BRAND(state, { payload }) {},
};
const actions = {
    // 获取品牌分类
    getProductBrand(store, payload) {
        return this.$http("getBrandList").then((response) => {
            let list = response.data || [];
            let arr = [];
            list.map((item) => {
                arr.push({
                    label: item.name,
                    value: item.brand_id,
                    key: item.ID,
                    sort: item.sort,
                });
            });
            store.state.product_Brand = arr || [];
        });
    },

    // 获取商品分类
    getProductClassify(store, payload) {
        return this.$http("getTypeList").then((response) => {
            const option = response.data || {};
            const classify = option.classify || [];
            const classifyFilter = (list) => {
                const result = list.filter((i) => i.is_show == 1);
                // 排序
                result.sort((a, b) => a.sort - b.sort);
                result.map((item) => {
                    if (item.child && item.child.length > 0) {
                        item.child = classifyFilter(item.child);
                    }
                });
                return result;
            };

            store.state.new_arrivals_count = option.new_arrivals_count || 0;
            store.state.product_classify = classifyFilter(classify);
        });
    },
};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions,
};
